package com.shenghua.controller;

import com.alibaba.fastjson.JSON;
import com.shenghua.dao.model.RoleLimitX;
import com.shenghua.dao.model.User;
import com.shenghua.service.RoleLimitService;
import com.shenghua.utils.beans.Dto;
import com.shenghua.utils.beans.ErrorCode;
import com.shenghua.utils.sysutil.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 用于用户界面动态显示
 * Created by Administrator on 2019/4/5.
 */
@Controller
@RequestMapping("/")
public class RolesLimitsController {
    Logger logger = LoggerFactory.getLogger(RolesLimitsController.class);

    @Resource
    private RoleLimitService roleLimitService;

    //查询角色菜单信息
    @RequestMapping(value = "/queryMenuLimitByRoleId/normal",method = RequestMethod.POST,
            produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryMenuLimitByRoleId_Normal(@RequestParam Map<String,Object> params) {
        Dto<List<RoleLimitX>> dto = new Dto<>();
        String roleId = "";
        if(MapUtils.isNotEmpty(params)) {
            roleId = null == params.get("roleId") ? "" : params.get("roleId").toString();
        }
        dto = roleLimitService.queryMenuLimitByRoleId_normal(roleId);
        if(dto.isSuccess()) {
            logger.info("[RolesLimitsController] queryMenuLimitByRoleId_Normal 查询角色菜单信息成功！");
        } else {
            logger.error("[RolesLimitsController] queryMenuLimitByRoleId_Normal 查询角色菜单信息失败！");
        }
        return JSON.toJSONString(dto);
    }

    //查询角色菜单信息
    @RequestMapping(value = "/queryButtonLimitByParentCode/normal",method = RequestMethod.POST,
            produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryButtonLimitByParentCode_Normal(
            @RequestParam Map<String,Object> params,
            HttpServletRequest request) {
        Dto<List<RoleLimitX>> dto = new Dto<>();
        String roleId = "";
        String parentCode = "";
        User user = (User)request.getSession().getAttribute("userSession");
        if(null == user) {
            logger.debug("[RolesLimitsController] queryButtonLimitByParentCode_Normal 用户未登录");
            dto.ReturnFail(null, ErrorCode.USER_NOT_LOGIN);
            return JSON.toJSONString(dto);
        }

        if(null == user.getRoleId()) {
            logger.debug("[RolesLimitsController] queryButtonLimitByParentCode_Normal 用户无角色权限");
            dto.ReturnSuccess(null, ErrorCode.PARAMETER_IS_NULL);
            return JSON.toJSONString(dto);
        }

        if(null == params || MapUtils.isEmpty(params)) {
            logger.info("[RolesLimitsController] queryButtonLimitByParentCode_Normal 参数不能为空");
            dto.ReturnFail(null, ErrorCode.PARAMETER_IS_NULL);
            return JSON.toJSONString(dto);
        }
        parentCode = params.get("parentCode").toString();
        roleId = user.getRoleId().toString();
        logger.debug("[RolesLimitsController] queryButtonLimitByParentCode_Normal roleId:{},parentCode:{}", roleId, parentCode);
        dto = roleLimitService.queryButtonLimit_normal(roleId,parentCode);
        if(dto.isSuccess()) {
            logger.info("[RolesLimitsController] queryButtonLimitByParentCode_Normal 查询角色按钮信息成功！");
        } else {
            logger.error("[RolesLimitsController] queryButtonLimitByParentCode_Normal 查询角色按钮信息失败！");
        }
        logger.info("[RolesLimitsController] queryButtonLimitByParentCode_Normal dto:{}", JSON.toJSONString(dto));
        return JSON.toJSONString(dto);
    }

    //查询角色所有权限信息
    @RequestMapping(value = "/queryLimitByRole/normal",method = RequestMethod.POST,
            produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryLimitByRole_Normal(HttpServletRequest request) {
        Dto<List<RoleLimitX>> dto = new Dto<>();
        User user = (User)request.getSession().getAttribute("userSession");
        if(null == user) {
            logger.debug("[RolesLimitsController] queryLimitByRole_Normal 用户未登录");
            dto.ReturnFail(null, ErrorCode.USER_NOT_LOGIN);
            return JSON.toJSONString(dto);
        }

        if(null == user.getRoleId()) {
            logger.debug("[RolesLimitsController] queryLimitByRole_Normal 用户无角色权限");
            dto.ReturnSuccess(null, ErrorCode.PARAMETER_IS_NULL);
            return JSON.toJSONString(dto);
        }

        /*if(StringUtils.isBlank(roleId)) {
            logger.info("[RolesLimitsController] queryLimitByRoleId_Normal 参数不能为空");
            dto.ReturnFail(null, ErrorCode.PARAMETER_IS_NULL);
            return JSON.toJSONString(dto);
        }*/
        String roleId = user.getRoleId().toString();
        logger.debug("[RolesLimitsController] queryLimitByRole_Normal roleId:{}", roleId);
        dto = roleLimitService.queryLimitByRoleId_normal(roleId);
        if(dto.isSuccess()) {
            logger.info("[RolesLimitsController] queryLimitByRole_Normal 查询角色按钮信息成功！");
        } else {
            logger.error("[RolesLimitsController] queryLimitByRole_Normal 查询角色按钮信息失败！");
        }
        logger.info("[RolesLimitsController] queryLimitByRole_Normal dto:{}", JSON.toJSONString(dto));
        return JSON.toJSONString(dto);
    }
}
